<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<title>e网生活管理后台</title>
	<?php 
	include("common/conn.php"); 
	include("./tool/UpFileTool.class.php");
	if($_SESSION['userid']==""){
		echo "<script>window.location.href='login.php';</script>";
		exit;
	} 

if($_GET['action']=="ok"){
	if ($_FILES) {
		$UpFileTool=new UpFileTool('upfile');
		$UpFileTool->sumbImage($w=640, $h=360);
		$UpFileTool->upFile();
		$images = $UpFileTool->sumbImage;
		$srcimages = $UpFileTool->srcImage;
	}

	// if($_REQUEST['shopid']==""){
	// 	echo "<script>alert('没有选择广告商品哦！');</script>"; echo "<script>window.location.href='shequ.php';</script>"; exit();
	// }
	$type = $_REQUEST[type];
	switch ($type) {
		case 0:
			// 查找市场信息
			$query = mysql_query("select name from city where id=".$_REQUEST[cityid][0]);
			$city = mysql_fetch_assoc($query);
			// 查找市场信息 end
			
			$query = mysql_query("INSERT INTO ad (type,srcimages,images,content,cityid,city) VALUES ('".$type."','".$srcimages."','".$images."','".$_REQUEST[content]."','".$_REQUEST[cityid][0]."','".$city[name]."')");
			break;
		
		case 1:
			// 查找市场信息
			$query = mysql_query("select name from city where id=".$_REQUEST[cityid][1]);
			$city = mysql_fetch_assoc($query);
			// 查找市场信息 end
			
			$query = mysql_query("INSERT INTO ad (type,srcimages,images,cateid,cityid,city) VALUES ('".$type."','".$srcimages."','".$images."','".$_REQUEST[cateid]."','".$_REQUEST[cityid][1]."','".$city[name]."')");
			break;

		case 2:
			// 根据shopid查找市场信息
			$query = mysql_query("select id,name from city where id=(select city from shop where shopid=".$_REQUEST[shopid].")");
			$city = mysql_fetch_assoc($query);
			// 根据shopid查找市场信息 end
			
			$query = mysql_query("INSERT INTO ad (type,srcimages,images,shopid,cityid,city) VALUES ('".$type."','".$srcimages."','".$images."','".$_REQUEST[shopid]."','".$city[id]."','".$city[name]."')");
			break;
	}
	// $srat=mysql_query("INSERT INTO ad(images,srcimages,content,cityid,city,shopid) VALUES ('$images','$srcimages','$_POST[content]','$_POST[cityid]','$_POST[city]','$_REQUEST[shopid]')");
	echo "<script>alert('新增广告成功!');window.location.href='ad.php';</script>";

}

if($_GET['ch']=="ok"){ 
	// if($_REQUEST['shopid']=="")
	// {
	// 	echo "<script>alert('没有选择广告店铺哦！');</script>"; echo "<script>window.location.href='shequ.php';</script>"; exit();
	// }
	// $srat1=mysql_query("update ad set content='".$_REQUEST['content']."',shopid='".$_REQUEST['shopid']."',cityid='".$_REQUEST['cityid']."',city='".$_REQUEST['city']."' where id='".$_REQUEST['id']."'");
	$type = $_REQUEST[type];
	switch ($type) {

		case 0:
			// 查找市场信息
			$query = mysql_query("select name from city where id=".$_REQUEST[cityid][0]);
			$city = mysql_fetch_assoc($query);
			// 查找市场信息 end
			
			$query = mysql_query("UPDATE ad set type=0,content='".$_REQUEST[content]."',cityid='".$_REQUEST[cityid][0]."',city='".$city[name]."' where id=".$_REQUEST[id]);
			break;
		
		case 1:
			// 查找市场信息
			$query = mysql_query("select name from city where id=".$_REQUEST[cityid][1]);
			$city = mysql_fetch_assoc($query);
			// 查找市场信息 end
			
			$query = mysql_query("UPDATE ad set type=1,cateid='".$_REQUEST[cateid]."',cityid='".$_REQUEST[cityid][1]."',city='".$city[name]."' where id=".$_REQUEST[id]);
			break;

		case 2:
			// 根据shopid查找市场信息
			$query = mysql_query("select id,name from city where id=(select city from shop where shopid=".$_REQUEST[shopid].")");
			$city = mysql_fetch_assoc($query);
			// 根据shopid查找市场信息 end
			$query = mysql_query("UPDATE ad set type=2,shopid='".$_REQUEST[shopid]."',cityid='".$city[id]."',city='".$city[name]."' where id=".$_REQUEST[id]);
			break;
	}
	if ($_FILES[upfile][tmp_name]!='') {
		$UpFileTool=new UpFileTool('upfile');
		$UpFileTool->sumbImage($w=640, $h=360);
		$UpFileTool->upFile();
		$images = $UpFileTool->sumbImage;
		$srcimages = $UpFileTool->srcImage;
		$srat1=mysql_query("update ad set images='".$images."',srcimages='".$srcimages."' where id='".$_REQUEST['id']."'");
	}
	echo "<script>alert('修改广告信息成功!');window.location.href='ad.php';</script>";
}
// 查找广告信息
if($_REQUEST['id']!=""){

	$query = mysql_query("select * from ad where id='".$_GET['id']."'");
	$ad = mysql_fetch_array($query);
}
// 查找广告信息 end

// 查询广告商家
// $query = mysql_query("select * from shop where shopid=".$_REQUEST[shopid]);
// $shop = mysql_fetch_assoc($query);
// 查询广告商家 end
// 查找商家列表
$query = mysql_query("select shopid,name from shop");
while ($row = mysql_fetch_assoc($query)) {
	$allshop[] = $row;
}
// 查找商家列表 end
// 查询商店分类列表 
$query = mysql_query("select cateid,name from categroy");
while ($row = mysql_fetch_assoc($query)) {
	$categroy[] = $row;
}
// 查询商店分类列表 end
// 查询市场列表
$query = mysql_query("select id,name from city");
while ($row = mysql_fetch_assoc($query)) {
	$city[] = $row;
}
// 查询市场列表 end
?>
<script type="text/javascript" src="js/jquery.min.js"></script>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link href="css/style2.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" href="write/themes/default/default.css" />
	<script charset="utf-8" src="write/kindeditor-min.js"></script>
	<script charset="utf-8" src="write/lang/zh_CN.js"></script>
	<script>
		var editor;
		KindEditor.ready(function(K) {
			editor = K.create('textarea[name="content"]', {
				allowFileManager : true
			});
			K('input[name=getHtml]').click(function(e) {
				alert(editor.html());
			});
			K('input[name=isEmpty]').click(function(e) {
				alert(editor.isEmpty());
			});
			K('input[name=getText]').click(function(e) {
				alert(editor.text());
			});
			K('input[name=selectedHtml]').click(function(e) {
				alert(editor.selectedHtml());
			});
			K('input[name=setHtml]').click(function(e) {
				editor.html('<h3>Hello KindEditor</h3>');
			});
			K('input[name=setText]').click(function(e) {
				editor.text('<h3>Hello KindEditor</h3>');
			});
			K('input[name=insertHtml]').click(function(e) {
				editor.insertHtml('<strong>插入HTML</strong>');
			});
			K('input[name=appendHtml]').click(function(e) {
				editor.appendHtml('<strong>添加HTML</strong>');
			});
			K('input[name=clear]').click(function(e) {
				editor.html('');
			});
		});


	</script>
</head>

<body>

	<!--header-->
	<?php include("common/header.php");?>
	<!--header end-->


	<!-- staqrt subnav -->
	<div id="sub_nav">
	</div>
	<div class="container_12" style=" border-bottom:100px;">
		<form action="?<?php if($_GET['id']!=""){echo "ch=ok"; ?>&id=<?php echo $_GET['id'];}else{echo "action=ok";}?>" method="post" enctype="multipart/form-data"  >
			<!--收货信息-->
			<div class="car_mode" >

				<div class="indexfloat" style="width:75%; margin-left:5%;">
					<label>广告</label>
					<span style="font-size: 12px;">(为了保持APP版面的协调性，请上传640*360像素大小的图片)</span>
				</div> 

				<div class="indexfloat" style="width:79%;" >
					<input name="upfile" type="file" /><img src="<?php   echo $ad[images];?>" width="150" height="auto"/>
				</div>

			</div>

			<!--收货信息 end-->
			<!--收货信息-->
			<div class="car_mode" >

				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>广告图类型</label>
				</div>

				<div class="indexfloat" style="width:79%;" >
					<input <?php if ($ad[type] == 0) { ?> checked="checked" <?php } ?> type="radio" name="type" value="0">
					<label>图文详情</label>
					<input <?php if ($ad[type] == 1) { ?> checked="checked" <?php } ?> type="radio" name="type" value="1">
					<label>商家列表(关联 商店分类(商品主分类))</label>
					<input <?php if ($ad[type] == 2) { ?> checked="checked" <?php } ?> type="radio" name="type" value="2">
					<label>某一个商家</label>
				</div>

			</div>
			<!--收货信息 end-->
			<!-- 广告详情 -->
			<div class="car_mode contentType" <?php if ($ad[type] != 0) { ?> style="opacity:0;height:0;" <?php } ?>>
				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>市场</label>
				</div>

				<div class="indexfloat" style="width:79%;" >
					<select name="cityid[]">
						<?php foreach ($city as $v) { ?>
						<option value="<?php echo $v[id]; ?>" <?php if ($v[id] == $ad[cityid]) { ?> selected <?php } ?>>
							<?php echo $v[name]; ?>
						</option>
						<?php } ?> 
					</select>
				</div>
				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>广告详情</label>
				</div>

				<div class="indexfloat" style="width:79%;" >
					<textarea name="content" style="height:500px;">
						<?php 
						if ($ad[type] == 0) {
							echo $ad[content];
						}
						?>
					</textarea>
				</div>

			</div>
			<!-- 广告详情 end -->
			<!-- 商店分类 -->
			<div class="car_mode categroyType" <?php if ($ad[type] != 1) { ?> style="display:none;" <?php } ?> >
				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>市场</label>
				</div>

				<div class="indexfloat" style="width:79%;" >
					<select name="cityid[]">
						<?php foreach ($city as $v) { ?>
						<option value="<?php echo $v[id]; ?>" <?php if ($v[id] == $ad[cityid]) { ?> selected <?php } ?>>
							<?php echo $v[name]; ?>
						</option>
						<?php } ?> 
					</select>
				</div>
				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>商店分类</label>
				</div>

				<div class="indexfloat" style="width:79%;" >
					<select name="cateid" class="select">
						<?php foreach ($categroy as $k => $v) { ?>
							<option <?php if ($ad[type] == 1 && $ad[cateid] == $v[cateid]) { ?> selected="selected" <?php } ?>value="<?php echo $v[cateid]; ?>"><?php echo $v[name]; ?></option>
						<?php } ?>
					</select>
				</div>

			</div>
			<!-- 商店分类 end -->
			<!-- 某一个商家 -->
			<div class="car_mode shopType" <?php if ($ad[type] != 2) { ?> style="display:none;" <?php } ?> >

				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>广告商家</label>
				</div>

				<div class="indexfloat" style="width:79%;" >
					<select name="shopid" class="select">
						<?php foreach ($allshop as $k => $v) { ?>
						<option <?php if ($ad[type] == 2 && $ad[shopid] == $v[shopid]) { ?> selected="selected" <?php } ?> value="<?php echo $v[shopid]; ?>"><?php echo $v[name]; ?></option>
						<?php } ?>
					</select>
				</div>

			</div>
			<!-- 某一个商家 end -->
			<!--收货信息-->
<!--  			<div class="car_mode" >
				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>广告商家:<?php echo $shop[name]; ?></label>
				</div> 
				<div class="indexfloat" style="width:15%; margin-left:5%;">
					<label>广告产品</label>
				</div> 

				<div class="indexfloat" style="width:79%;" >


					<?php 


					$rert37=mysql_query("select * from shop where shopid='".$_REQUEST[shopid]."' ");
					$sdg37=mysql_fetch_array($rert37);
					$imesg=array();
					$imes=$sdg37['images'];
					$imesg=explode("," ,$imes);

					?>

					<img style="width:200px;height:200px;" src="<?php echo $imesg[0]; ?>" /> <div class="youse2">
					 <a href="goods2.php?id=<?php echo $_REQUEST[id];?>">选择商品</a> 
				</div>  

			</div>

		</div>  -->

		<!--收货信息 end-->



		<!--收货信息-->
		<div class="car_mode" >



			<div class="indexfloat" style="width:79%;" >


				<?php 
				$rert37=mysql_query("select * from city where id =(select city from shop where shopid='".$_REQUEST[shopid]."' )");
				$sdg37=mysql_fetch_array($rert37);?>

<!-- 				<input name="city" type="hidden"  value="<?php echo $sdg37[city];?>">
				<input name="cityid" type="hidden"  value="<?php echo $sdg37[id];?>"> -->


			</div>

		</div>

		<!--收货信息 end-->



		<!--收货信息-->
		<div class="car_mode" style="margin-bottom:100px;" >
<!-- 			<input name="shopid" type="hidden" value="<?php echo $_REQUEST[shopid];?>" /> -->
			<input name="" type="submit" class="submit"  value="提交" />
		</div>

		<!--收货信息 end-->



	</form>          
</div>

<!-- end content wrap -->

</form>
</body>
</html>

<script type="text/javascript">
	function zi(n)
	{
		for(var i=1;i<=50;i++)
		{
			document.getElementById(i).style.display="none"; 
			document.getElementById(n).style.display="block";  
		}

	}

	function zis(n)
	{
		for(var i=100;i<=150;i++)
		{
			document.getElementById(i).style.display="none"; 
			document.getElementById(n).style.display="block";  
		}

	}
	// 选择不同的广告类型
	$('input[name="type"]').change(function(){
		var type = $(this).val();
		if (type == 0) {
			$('.contentType').css('opacity','1');
			$('.contentType').css('height','auto');
			$('.categroyType').hide();
			$('.shopType').hide();
		}else if (type == 1) {
			$('.contentType').css('opacity','0');
			$('.contentType').css('height','0');
			$('.categroyType').show();
			$('.shopType').hide();
		}else if (type == 2) {
			$('.contentType').css('opacity','0');
			$('.contentType').css('height','0');
			$('.categroyType').hide();
			$('.shopType').show();
		};
	})
	// 选择不同的广告类型 end
</script>